<template>
	<view class="package">
		<view class="desc">
			<view class="content">

				<view class="row-card">
					<image src="@/static/img/5c5fe0cb89c3cab185c07b2d5975024a.jpg"></image>
				</view>

				<view class="row-content">
					<text>小丑模拟器</text>
					<text>本模拟器由一次麦联合出品！一次麦祝大家玩的愉快！本软件不能聊天！本软件不能聊天！本软件不能聊天！</text>

					<view class="row-input">
						<input v-model="nickname" placeholder="请输入Ta的昵称" />
					</view>

					<button @click="toPage">
						<span>一键变小丑</span>
					</button>
					<text class="Tip">注意：内容由人工智能生成，仅供参考</text>
				</view>

			</view>

			<view class="row-share">
				<view @click="uploadTaAvatar">
					<img :src="taAvatar"></img>
					<text>上传Ta的头像</text>
				</view>
				<view @click="uploadYourAvatar">
					<img :src="yourAvatar"></img>
					<text>上传你的头像</text>
				</view>
				<view>
					<img src="@/static/img/ijpg.jpg"></img>
					<text>群:1032780992</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';

const nickname = ref('');
const yourAvatar = ref('');
const taAvatar = ref('');

const toPage = async () => {
	if (!nickname.value) {
		await uni.showModal({
			title: '提示',
			content: '请输入昵称',
			showCancel: false
		});
		return;
	}

	uni.setStorageSync('taName', nickname.value);

	uni.navigateTo({
		url: `/pages/chat/chat`
	});
};

const handleRefresh = async () => {
	yourAvatar.value = uni.getStorageSync('yourAvatar') || '/static/img/ijpg.jpg';
	taAvatar.value = uni.getStorageSync('taAvatar') || '/static/img/taImg.jpg';
	nickname.value = uni.getStorageSync('taName');
};

const uploadTaAvatar = async () => {
	const res = await uni.chooseImage({
		count: 1,
		sizeType: ['compressed']
	}) as UniApp.ChooseImageSuccessCallbackResult;

	// 使用fetch获取Blob对象
	const response = await fetch(res.tempFilePaths[0]);
	const blob = await response.blob();
	
	const reader = new FileReader();
	reader.readAsDataURL(blob);
	await new Promise<void>(resolve => reader.onload = () => resolve());
	taAvatar.value = reader.result as string;
	uni.setStorageSync('taAvatar', reader.result);
}

const uploadYourAvatar = async () => {
	const res = await uni.chooseImage({
		count: 1,
		sizeType: ['compressed']
	}) as UniApp.ChooseImageSuccessCallbackResult;

	// 使用fetch获取Blob对象
	const response = await fetch(res.tempFilePaths[0]);
	const blob = await response.blob();
	
	const reader = new FileReader();
	reader.readAsDataURL(blob);
	await new Promise<void>(resolve => reader.onload = () => resolve());
	yourAvatar.value = reader.result as string;
	uni.setStorageSync('yourAvatar', reader.result);
}

onMounted(() => {
	handleRefresh();
});
</script>

<style scoped lang="scss">
.package {
	width: 100%;
	height: 100%;
	display: flex;
	justify-content: center
}

.desc {
	height: 100%;
	max-width: 520px;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.content {
	flex: 1;
	background-color: #F6F6F6;
	// height: 100%;
	padding-left: 16px;
	padding-right: 16px;
	// padding-top: 10%;
	overflow: hidden; // 防止外边距折叠现象				
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.row-card {
	// margin-top: 100%;
	margin-bottom: 1vh;
	// margin-bottom: 30px;
	display: flex;
	justify-content: center;
	width: 100%;

	image {
		border-radius: 20px;
		width: 50%;
		height: 100%;
		aspect-ratio: 1/1;
	}
}

.row-content {
	display: flex;
	flex-direction: column;
	align-items: center;
	text-align: center;

	text:nth-child(1) {
		// margin-top: 4%;
		margin-bottom: 1vh;
		font-size: 24px;
		font-weight: 700;
		letter-spacing: 0px;
		line-height: 34.75px;
		color: rgba(0, 0, 0, 1);
	}

	text:nth-child(2) {
		// margin-top: 4%;
		font-size: 16px;
		font-weight: 400;
		letter-spacing: 0.5px;
		line-height: 23.17px;
		color: rgba(125, 125, 125, 1);
	}

	button {
		margin-top: 1vh;
		width: 100%;
		height: 5vh;
		border-radius: 16px;
		background-color: rgba(255, 216, 24, 1);

		display: flex;
		align-items: center;
		justify-content: center;

		::v-deep span {
			font-size: 16px;
			font-weight: 700;
			letter-spacing: 3px;
			line-height: 23.17px;
			color: rgba(255, 255, 255, 1);

			/* 添加文字描边效果 */
			text-shadow:
				-1px -1px 0 #000,
				1px -1px 0 #000,
				-1px 1px 0 #000,
				1px 1px 0 #000;
		}
	}

	text:nth-child(4) {
		margin-top: 1vh;
		font-size: 12px;
		font-weight: 400;
		letter-spacing: 0px;
		line-height: 17.38px;
		color: rgba(140, 140, 140, 1);
	}
}

.row-share {
	width: 100%;

	//position: fixed; // 固定定位
	bottom: 0; // 底部对齐

	padding-bottom: 4vh;

	display: flex;
	justify-content: space-around;
	align-items: center;

	font-size: 14px;
	font-weight: 500;
	letter-spacing: 0px;
	line-height: 20.27px;
	color: rgba(0, 0, 0, 1);

	view {
		display: flex;
		flex-direction: column;
		align-items: center;

		img {
			width: 80px !important;
			height: 80px !important;
			border-radius: 200px;
			// margin-bottom: 6px;
		}
	}
}

.row-input {
	margin-top: 1vh;
	width: 100%;

	& :nth-child(1) {
		font-size: 16px;
		font-weight: 400;
		letter-spacing: 0.5px;
	}

	input:nth-child(1) {
		padding-left: 16px;
		padding-right: 16px;
		height: 5vh;
		width: 90%;
		border-radius: 16px;
		background: rgba(255, 255, 255, 1);
		color: #000;
	}

}

.Tip {
	// margin-top: 5px;
	color: rgba(125, 125, 125, 1);
	font-size: 12px;
}
</style>